﻿namespace iFinance.Modules.Authorization.ViewModels
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Windows.Input;
    using iFinance.Entity;
    using iFinance.IDAL;
    using iFinance.Infrastructure;
    using iFinance.Infrastructure.ViewModels;
    using Microsoft.Practices.Unity;

    /// <summary>
    /// 用户注册ViewModel
    /// </summary>
    public class RegisterViewModel : ValidationNotificationObject
    {
        #region Property

        private string userID = string.Empty;

        [Display(Name = "账户", Description = "账户必须为4-16位")]
        public string UserID
        {
            get { return this.userID; }
            set
            {
                if (IsValidUserID(value) && this.userID != value)
                {
                    this.userID = value;
                    RaisePropertyChanged(() => this.UserID);
                }
            }
        }

        private string password = string.Empty;

        [Display(Name = "密码", Description = "密码必须为6-16位")]
        public string Password
        {
            get { return this.password; }
            set
            {
                if (ValidStringLength(() => this.Password, value, Resources.PasswordMustBetween6And16, 6, 16)
                    && this.password != value)
                {
                    this.password = value;
                    RaisePropertyChanged(() => this.Password);
                }
            }
        }

        private string passwordConfirm = string.Empty;

        [Display(Name = "再次输入", Description = "重复输入密码")]
        public string PasswordConfirm
        {
            get { return this.passwordConfirm; }
            set
            {
                if (Valid(() => this.PasswordConfirm, Resources.PasswordNotMatch,
                    () =>
                    {
                        return !string.IsNullOrEmpty(value) && value.Equals(Password);
                    }) && this.passwordConfirm != value)
                {
                    this.passwordConfirm = value;
                    RaisePropertyChanged(() => this.PasswordConfirm);
                }
            }
        }

        private string nickName = string.Empty;
        [Display(Name = "昵称", Description = "昵称不能为空")]
        public string NickName
        {
            get { return this.nickName; }
            set
            {
                if (ValidStringNotEmpty(() => this.NickName, value, Resources.NickNameCantEmpty)
                    && this.nickName != value)
                {
                    this.nickName = value;
                    RaisePropertyChanged(() => this.NickName);
                }
            }
        }

        private string question = string.Empty;
        [Display(Name = "安全问题", Description = "安全问题不能为空")]
        public string Question
        {
            get { return this.question; }
            set
            {
                if (ValidStringNotEmpty(() => this.Question, value, Resources.SecurityQuestionCantEmpty)
                    && this.question != value)
                {
                    this.question = value;
                    RaisePropertyChanged(() => this.Question);
                }
            }
        }

        private string answer = string.Empty;
        [Display(Name = "回答", Description = "回答不能为空")]
        public string Answer
        {
            get { return this.answer; }
            set
            {
                if (ValidStringNotEmpty(() => this.Answer, value, Resources.AnswerCantEmpty) && this.answer != value)
                {
                    this.answer = value;
                    RaisePropertyChanged(() => this.Answer);
                }
            }
        }

        #endregion

        #region Command

        #region RegisterCommand

        private ICommand regCommand = null;

        public ICommand RegCommand
        {
            get { return this.regCommand; }
            set
            {
                if (regCommand != value)
                {
                    this.regCommand = value;
                    RaisePropertyChanged(() => this.RegCommand);
                }
            }
        }

        #endregion

        #endregion

        #region Method

        /// <summary>
        /// 判断用户名是否有效
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool IsValidUserID(string value)
        {
            if (ValidStringLength(() => this.UserID, value, Resources.UserIDMustBetween4And16, 4, 16))
            {
                //判断用户名是否存在  
                var dao = UnityHelper.Container.Resolve<IUserDAO>();
                if (dao != null)
                {
                    if (dao.IsUserExist(value))
                    {
                        AddError(() => this.UserID, Resources.UserIDExist);
                        return false;
                    }
                    else
                    {
                        RemoveError(() => this.UserID, Resources.UserIDExist);
                    }
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        public bool Register()
        {
            if (!this.HasErrors)
            {
                User user = new User();
                user.UserID = this.UserID;
                user.NickName = this.NickName;
                user.Password = this.Password;
                user.Question = this.Question;
                user.Answer = this.Answer;
                user.RegisterTime = DateTime.Now;

                var dao = UnityHelper.Container.Resolve<IUserDAO>();
                if (dao != null)
                {
                    return dao.AddUser(user);
                }
            }
            return false;
        }

        #endregion
    }
}
